﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;
namespace jnxc.Web.materialinfo
{
    /// <summary>
    /// GetMaterial 的摘要说明
    /// </summary>
    public class GetMaterial : IHttpHandler, IRequiresSessionState
    {
        jnxc.BLL.materialinfo bll = new jnxc.BLL.materialinfo();
        
        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "instock")
                instock(context);
            else if (ac == "outstock")
                outstock(context);
        }
        public void outstock(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                decimal num = 0;
                int mid = 0;
                string snum = context.Request.Form["snum"];
                string midin = context.Request.Form["midin"];
                if (midin != "")
                {
                    mid = int.Parse(midin);
                }
                if (snum != "")
                {
                    num = decimal.Parse(snum);
                }
                jnxc.Model.instock model = new jnxc.Model.instock();
                model.mid = mid;
                model.snum = num;
                model.uid = uid;
                model.otype = 1;

                jnxc.BLL.instock inbll = new BLL.instock();
                if (inbll.Add(model) > 0)
                {
                    jnxc.BLL.stock sbll = new BLL.stock();
                    int i = sbll.Add(uid, mid, num,1);
                    if (i == 1)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();

                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void instock(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                decimal num = 0;
                int mid = 0;
                string snum = context.Request.Form["snum"];
                string midin = context.Request.Form["midin"];
                if (midin != "")
                {
                    mid = int.Parse(midin);
                }
                if (snum != "")
                {
                    num = decimal.Parse(snum);
                }
                jnxc.Model.instock model = new jnxc.Model.instock();
                model.mid = mid;
                model.snum = num;
                model.uid = uid;                
                model.otype =0;                
                
                jnxc.BLL.instock inbll = new BLL.instock();
                if (inbll.Add(model) > 0)
                {
                    jnxc.BLL.stock sbll = new BLL.stock();
                    int i=sbll.Add(uid, mid, num, 0);
                    if (i == 1)
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();

                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["mid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);            
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int mid = 0;
                string uidstr = context.Request.Form["mid"];
                if (uidstr != "")
                    mid = int.Parse(uidstr);
                string mbh = context.Request.Form["mbh"];
                string mname = context.Request.Form["mname"];
                string mtype = context.Request.Form["mtype"];
                string mfun = context.Request.Form["mfun"];
                string mpara = context.Request.Form["mpara"];
                
                jnxc.Model.materialinfo model = new jnxc.Model.materialinfo();
                model.mbh = mbh;
                model.mname = mname;
                model.mtype = mtype;
                model.mfun = mfun;
                model.mpara = mpara;
                model.mid = mid;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string mbh = context.Request.Form["mbh"];
                string mname = context.Request.Form["mname"];
                string mtype = context.Request.Form["mtype"];
                string mfun = context.Request.Form["mfun"];
                string mpara = context.Request.Form["mpara"];
                jnxc.Model.materialinfo model = new jnxc.Model.materialinfo();
                model.mbh = mbh;
                model.mname = mname;
                model.mtype = mtype;
                model.mfun = mfun;
                model.mpara = mpara;
                model.uid = uid;
                if (bll.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        public void Query(HttpContext context)
        {


            context.Response.ContentType = "text/plain";
             int uid = Web.Admin.myinfo.getID(context);
             if (uid != 0)
             {
                 string uname, utruename, st;
                 uname = utruename = st = "";
                 //获取前台传来的值 
                 if (null != context.Request.Form["search_type"])
                 {//获取前台传来的值  
                     st = context.Request.Form["search_type"].ToString().Trim();
                 }
                 if (null != context.Request.Form["search_value"])
                 {//获取前台传来的值  
                     uname = context.Request.Form["search_value"].ToString().Trim();
                 }
                 if (st == "username")
                 {
                     utruename = uname;
                     uname = "";
                 }
                 //================================================================  
                 //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                 int pageRows, page;
                 pageRows = 10;
                 page = 1;
                 string order, sort, oderby; order = sort = oderby = "";
                 if (null != context.Request.Form["rows"])
                 {
                     pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                 }
                 if (null != context.Request.Form["page"])
                 {
                     page = int.Parse(context.Request.Form["page"].ToString().Trim());
                 }
                 if (null != context.Request.Form["sort"])
                 {
                     order = context.Request.Form["sort"].ToString().Trim();
                 }
                 if (null != context.Request.Form["order"])
                 {
                     sort = context.Request.Form["order"].ToString().Trim();
                 }
                 StringBuilder strWhere = new StringBuilder();
                 if (uname != "")
                 {
                     strWhere.AppendFormat(" mname like '%{0}%' and ", uname);
                 }
                 if (utruename != "")
                 {
                     strWhere.AppendFormat(" mbh like '%{0}%' and ", utruename);
                 }
                 strWhere.AppendFormat(" uid={0} and ", uid);
                 //删除多余的and  
                 int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                 if (startindex >= 0)
                 {
                     strWhere.Remove(startindex, 3);//删除多余的and关键字  
                 }
                 if (sort != "" && order != "")
                 {
                     oderby = order + " " + sort;
                 }
                 DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                 int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                 string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                 context.Response.Write(strJson);//返回给前台页面  
                 context.Response.End();
             }
             else
             {
                 context.Response.Write("L");//返回给前台页面  
                 context.Response.End();

             }
        }  

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}